package com.my.controller.System;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.my.bean.*;
import com.my.service.RoleService;
import com.my.service.RoleUserService;
import com.my.util.SessionUtil;
import netscape.javascript.JSObject;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.sql.SQLException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * <p>
 * 角色表 前端控制器
 * </p>
 *
 * @author 张子艺
 * @since 2018-08-26
 */
@Controller
@RequestMapping("/role")
public class RoleController {
    @Autowired
    RoleService roleService;

    //子页面请求
    @RequestMapping("/main.html")
    public String home(){
        return "/System/role";

    }
    //新增角色  和菜单关联
    @RequestMapping("/insertRole")
    @ResponseBody
    public String insertRole( String param,HttpSession session,String appoint){
        Role role=JSONObject.parseObject(param,Role.class);
        System.out.println("role:"+role);
        Long areaid=((User)session.getAttribute("user")).getAreaId();
        role.setAreaid(areaid);
        System.out.println("areaid:"+areaid);

//        把实体类转换成一个JSON 字符串
        String json=JSONObject.toJSONString(role);
        System.out.println("json:"+json);

        Role role1 = new Role();
   // System.out.println("appoint= "+appoint);
        if(appoint.equals("可以")){
            if(roleService.insertRoleAndMenu(param)){
              boolean aa= roleService.updateRoleValue();
              if (aa) {
                 // System.out.println("走了可以");
                  return  "新增成功";
              }else {
                  return "失败";
              }

            }
        }else{
            if(roleService.insertRoleAndMenu(param)){
                //System.out.println("走了不可以");
                return  "新增不可以成功";
            }
        }
        return "服务器错误...请稍后再试";
    }

    //查询所有角色  如果带分页参数 就分页  如果不带分页参数  就不分页
    @RequestMapping("/findRole")
    @ResponseBody
    public ResultPage<Role> findRole(VoPage voPage, Role role, HttpSession session){

          boolean admin=false;
        //如果事超级管理员  即可查看所有的房间  判断是否为超级管理员
         if(SessionUtil.getSessionUser(session).getRoles().getRoleValue().equals("9f77d5a8-75c6-4d06-adf9-a37e727d491a")){
            admin=true;
        }
        Long areaid=((User)session.getAttribute("user")).getAreaId();
        ResultPage<Role> resultPage=roleService.selectMyPage(voPage,role,areaid,admin);
        return resultPage;
    }

    //删除选中的角色
   /*@RequestMapping("/deleteRole")
   @ResponseBody
   public String deleteRole(String param){
       try {
           return roleService.deleteRole(param);
       } catch (SQLException e) {
           return "失败";
       }
   }*/


    //删除
    @RequestMapping("/deleteRole")
    @ResponseBody
    public String deleteRole(Long id)   {
        try {
            return roleService.deleteRole(id);
        } catch (SQLException e) {
            return "删除失败";
        }
    }
    @RequestMapping("/Role_add")
    public String Role_add (){
        return "/Alert/Role_add";
    }


    /**
     * 指定人可以添加房间
     */
//    @RequestMapping("/updateRoleValue")
//    @ResponseBody
//    public String updateRoleValue(String appoint) {
//        if (roleService.updateRoleValue(appoint)) {
//            return "修改成功";
//        } else {
//            return "修改失败";
//        }
//    }



}
